package com.lzb.newloan.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzb.newloan.BorrowDetailActivity;
import com.lzb.newloan.InvestPersonPayActivity;
import com.lzb.newloan.R;
import com.lzb.newloan.pojo.BorrowPersonPojo;
import com.lzb.newloan.utils.AlertDialog;
import com.lzb.newloan.utils.BaseApplication;
import com.lzb.newloan.utils.CircularImage;
import com.lzb.newloan.utils.Formatdou;
import com.lzb.newloan.utils.BaseApplication;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

public class InvestAdapter extends BaseAdapter {

    private ArrayList<BorrowPersonPojo> pojoarray;
    private LayoutInflater mInflater;
    private BaseApplication app;
    private Context context;
    private TextView title_txt_center;
    private int num = 0, count;
    private CheckInterface checkInterface;

    public InvestAdapter(Context context, ArrayList<BorrowPersonPojo> pojo, TextView title_txt_center) {
        this.pojoarray = pojo;
        this.context = context;
        app = (BaseApplication) context.getApplicationContext();
        this.mInflater = LayoutInflater.from(context);
        this.title_txt_center = title_txt_center;
    }

    public ArrayList<BorrowPersonPojo> getArrayList() {
        return pojoarray;
    }

    public void setArrayList(ArrayList<BorrowPersonPojo> arrayList) {
        this.pojoarray = arrayList;
    }

    /**
     * 复选框接口
     */
    public interface CheckInterface {
        /**
         * 组选框状态改变触发的事件
         *
         * @param position  元素位置
         * @param isChecked 元素选中与否
         */
        void checkGroup(int position, boolean isChecked);
    }

    /**
     * 单选接口
     *
     * @param checkInterface
     */
    public void setCheckInterface(CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }

    @Override
    public int getCount() {
        if (pojoarray == null) {
            return 0;
        } else {
            return pojoarray.size();
        }
    }

    @Override
    public Object getItem(int arg0) {

        return pojoarray.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(final int postition, View convertView, ViewGroup arg2) {
        ViewHoler viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHoler();
            convertView = mInflater.inflate(R.layout.adapter_invest_item, null);
            viewHolder.img_icon = (CircularImage) convertView
                    .findViewById(R.id.img_icon);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.sex_icon = (ImageView) convertView
                    .findViewById(R.id.sex_icon);
            viewHolder.money = (TextView) convertView.findViewById(R.id.money);
            viewHolder.day = (TextView) convertView.findViewById(R.id.day);
            viewHolder.all_money = (TextView) convertView
                    .findViewById(R.id.all_money);
            viewHolder.check = (CheckBox) convertView.findViewById(R.id.check);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHoler) convertView.getTag();
        }
        final BorrowPersonPojo data = pojoarray.get(postition);
        if (data.getSex() == 0) {//0是男 1是女
            viewHolder.sex_icon.setImageResource(R.drawable.sex_b);
        } else {
            viewHolder.sex_icon.setImageResource(R.drawable.sex_g);
        }
        viewHolder.name.setText(data.getUsername());
        viewHolder.day.setText(data.getTime());
        viewHolder.all_money.setText(Formatdou.formatdou(data.getFk_money()) + "元");//本息
        viewHolder.money.setText(Formatdou.formatdou(data.getJk_money()) + "元");//借款金额
        viewHolder.check.setChecked(data.isChecked());
        ImageLoader.getInstance().displayImage(data.getLiving_photo(), viewHolder.img_icon);
        viewHolder.check.setTag(data);

        viewHolder.check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BorrowPersonPojo cartPojo = (BorrowPersonPojo) v.getTag();
                if (count < 5||((CheckBox) v).isChecked()==false) {
                    cartPojo.setChecked(((CheckBox) v).isChecked());
                    num = 0;
                    for (int i = 0; i < pojoarray.size(); i++) {
                        BorrowPersonPojo pojo = pojoarray.get(i);
                        if (num >= 5) {
                            dia();
                            break;
                        } else {
                            if (pojo.isChecked()) {
                                num = num + 1;
                            }
                        }
                    }
                } else {

                    cartPojo.setChecked(false);
                    dia();
                    notifyDataSetChanged();
                }
                count = num;

                title_txt_center.setText("已选择 " + num + "/5");

            }
        });


        //单选框按钮
//		viewHolder.check.setOnClickListener(
//				new View.OnClickListener() {
//					@Override
//					public void onClick(View v) {
//						data.setChecked(((CheckBox) v).isChecked());
//						checkInterface.checkGroup(postition, ((CheckBox) v).isChecked());//向外暴露接口
//					}
//				}
//		);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,
                        BorrowDetailActivity.class);
                intent.putExtra("jkid", data.getJkid());
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    public void dia() {
        AlertDialog ad = new AlertDialog(context);
        ad.builder();
        ad.setMsg("您已选取了5人进行投资，请问是否马上支付？");
        ad.setNegativeButton("再选选", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        ad.setPositiveButton("立即支付", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = "";
                for (int i = 0; i < pojoarray.size(); i++) {
                    BorrowPersonPojo pojo = pojoarray.get(i);
                    if (pojo.isChecked()) {
                        if (str.equals("")) {
                            str = pojo.getJkid();
                        } else {
                            str = str + "," + pojo.getJkid();
                        }
                    }
                }

                Intent intent = new Intent(context, InvestPersonPayActivity.class);
                intent.putExtra("jkid", str);
                context.startActivity(intent);
            }
        });
        ad.show();
    }

    public class ViewHoler {
        private ImageView img_icon, sex_icon;
        private CheckBox check;
        public TextView name, money, day, all_money;
    }

}
